package com.auto.learning.adapter;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alipay.sdk.util.LogUtils;
import com.auto.learning.bean.BannerItem;
import com.nostra13.universalimageloader.core.ImageLoader;

public class BannerAdapter extends PagerAdapter implements OnPageChangeListener {
	private Context mContext;
	private LinkedList<View> mViews;
	private List<Drawable> mList;
	private ViewPager mViewPager;

	public BannerAdapter(Context context, ViewPager viewPager, List<BannerItem> list) {
		this.mContext = context;
		mViewPager = viewPager;
		if (list != null) {
			mViews = new LinkedList<View>();
			ImageView view = new ImageView(context);
			view.setScaleType(ImageView.ScaleType.FIT_XY);
			BannerItem bannerItem = list.get(list.size() - 1);
			// 注意，如果不只1个，mViews比mList多两个（头尾各多一个）
			// 假设：mList为mList[0~N-1], mViews为mViews[0~N+1]
			// mViews[0]放mList[N-1], mViews[i]放mList[i-1], mViews[N+1]放mList[0]
			// mViews[1~N]用于循环；首位之前的mViews[0]和末尾之后的mViews[N+1]用于跳转
			// 首位之前的mViews[0]，跳转到末尾（N）；末位之后的mViews[N+1]，跳转到首位（1）
			if (list.size() > 1) { // 多于1个要循环
				for (BannerItem d : list) { // 中间的N个（index:1~N）
					ImageView v = new ImageView(context);
					v.setScaleType(ImageView.ScaleType.FIT_XY);
					ImageLoader.getInstance().displayImage(bannerItem.getBannerImg(), v);
					mViews.add(v);
				}
				/*// 最后一个（index:N+1）
				view = new ImageView(context);
				view.setScaleType(ImageView.ScaleType.FIT_XY);
				bannerItem = list.get(0);
				ImageLoader.getInstance().displayImage(bannerItem.getBannerImg(), view);
				mViews.add(view);*/
			}
		}
	}

	@Override
	public int getCount() {
		return mViews.size();
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		((ViewPager) container).removeView(mViews.get(position));
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		View view = mViews.get(position);
		container.addView(view);
		return view;
	}

	// 实现ViewPager.OnPageChangeListener接口
	@Override
	public void onPageSelected(int position) {
		LogUtils.d("onPageSelected", "onPageSelected:" + position);
		if (mViews.size() > 1) { // 多于1，才会循环跳转
			if (position < 1) { // 首位之前，跳转到末尾（N）
				position = mList.size(); // 注意这里是mList，而不是mViews
				mViewPager.setCurrentItem(position, false);
			} else if (position > mList.size()) { // 末位之后，跳转到首位（1）
				mViewPager.setCurrentItem(1, false); // false:不显示跳转过程的动画
				position = 1;
			}
		}
	}

	@Override
	public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
	}

	@Override
	public void onPageScrollStateChanged(int state) {
	}
}
